package com.gc.ip;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;


@SuppressWarnings("serial")
public class JSingleIPAddress extends JPanel
{
	private JIPAddress ipAddress = null;
	private JButton reportButton = null;
	
	public JSingleIPAddress()
	{
		this(null);
	}
	
	public JSingleIPAddress(JIPAddress ipAddress)
	{
		this.ipAddress = ipAddress;
		if(this.ipAddress == null)
		{
			this.ipAddress = new JIPAddress();
		}
		initGUI();
		addListeners();
	}

	private void initGUI()
	{
		reportButton = new JButton("IP Address Report");
		
		this.setLayout(new GridBagLayout());
		this.add(getLabel(), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
			GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		this.add(ipAddress, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
			GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		this.add(reportButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
			GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		
		this.setBorder(new TitledBorder("IP Address"));
	}
	private JLabel getLabel()
	{
		return new JLabel("IP Address/Prefix Length");
	}
	private void addListeners()
	{
		
	}
	
	@Override
	public void setEnabled(boolean enabled)
	{
		super.setEnabled(enabled);
		ipAddress.setEnabled(enabled);
		if(reportButton != null)
		{
			reportButton.setEnabled(enabled);
		}
	}
	
	public IPAddress getIPAddress()
	{
		return this.ipAddress.getIPAddress();
	}
	
	public void setIPAddress(IPAddress ipAddress)
	{
		this.ipAddress.setIPAddress(ipAddress);
	}
	
	public static void main(String[] args) throws Exception
	{
		JFrame frame = new JFrame("JSingleIPAddress Test");
		
		//Create a JIPAddress instance. It is actually a JPanel
		//The component has PrefixLength and allows both IPv4 and IPv6
		JIPAddress jIpAddress = new JIPAddress(true, IPType.IPv4_v6);
		
		//Create model data: a IPv6 address with prefix length
		IPAddress ip = new IPAddress("2001:0db8:85a3:08d3:1319:8a2e:0370::", 88);
		//Initialize the JIPAddress component with the above ip data
		jIpAddress.setIPAddress(ip);
		
		//Create a JSingleIPAddress component. It is actually a JPanel.
		//So you can easily add it to a Swing Container.
		JSingleIPAddress singleIPAddress = new JSingleIPAddress(jIpAddress);
		
		//Add the component to the frame
		frame.getContentPane().add(singleIPAddress);
		
		//GUI related operations
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(700, 100));
        frame.pack();
        frame.setVisible(true);
	}
}
